import { parse } from 'url';

// 部门管理
let tableListSysDepart = [
{
  id:'0',
  name:'XX学校',
},{// 二级
  id:'01',
  pid:'0',
  name:'校长室',
},{
  id:'02',
  pid:'0',
  name:'教研组',
},{
  id:'03',
  pid:'0',
  name:'党支部',
},{// 三级 校长室
  id:'010',
  pid:'01',
  name:'教务处',
},{
  id:'011',
  pid:'01',
  name:'政教处',
},{
  id:'012',
  pid:'01',
  name:'总务处',
},{// 三级 教研组
  id:'020',
  pid:'02',
  name:'语文教研组',
},{
  id:'021',
  pid:'02',
  name:'数学教研组',
},{
  id:'022',
  pid:'02',
  name:'体育教研组',
},{
  id:'030',
  pid:'03',
  name:'少先队',
},{
  id:'031',
  pid:'03',
  name:'团支部',
},{// 四级
  id:'0110',
  pid:'011',
  name:'年级组',
}]


export function getSysDepart(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = parse(url, true).query;

  let dataSource = [...tableListSysDepart];

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        [...dataSource].filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
      );
    });
    dataSource = filterDataSource;
  }

  if (params.no) {
    dataSource = dataSource.filter(data => data.no.indexOf(params.no) > -1);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export function postSysDepart(req, res, u, b) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const body = (b && b.body) || req.body;
  const { method, data } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListSysDepart = tableListSysDepart.filter(item => item.id!==data.id&&item.pid!==data.id);
      break;

    case 'post':// 新增
      tableListSysDepart.unshift({
        ...data,// pid name不变
        id:Math.round(Math.random()*100000),// getHash(data.name)//id唯一性。且
      });
      break;
    case 'patch':// 修改==》 部门修改名称或拖拽修改pid
      tableListSysDepart= tableListSysDepart.map(item=>{
        if(item.id === data.id){
          return Object.assign({},item,data)
        }
        return item;
      })
      break;
    default:
      break;
  }

  const result = {
    list: tableListSysDepart,
    pagination: {
      total: tableListSysDepart.length,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}



let tableDepartDetail =
[
  {
    id:'010',
    name:'教务处',
    rows:
    [
      {
        id:'0100',
        name:'张三',
        job:'文秘',
        tel:'894555122',
        memo:'xxxx',
      },
      {
        id:'0101',
        name:'测试2',
        job:'办公室主任',
        tel:'555664521242',
        memo:'',
      }
    ]
  },
  {
    id:'011',
    name:'政教处',
    rows:
    [
      {
        id:'0110',
        name:'测试22222',
        job:'不知道怎么编了',
        tel:'545455454',
        memo:'',
      },
      {
        id:'0111',
        name:'dsfsd',
        job:'当官的',
        tel:'54544',
        memo:'',
      }
    ]
  }
]


export function getSysDepartPerson(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = parse(url, true).query;

  let dataSource = [...tableDepartDetail];

  if (params.id) {
    dataSource = dataSource.filter(data => data.id === params.id);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource[0].rows,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}


export function postSysDepartPerson(req, res, u, b) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const body = (b && b.body) || req.body;
  const { method, pid ,data} = body;
  const result = {
    success:false,
    id:null,
  };
  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      const tableListDetaildel = tableDepartDetail.filter(item => item.id===pid);
      if(tableListDetaildel.length){
        tableListDetaildel[0].rows= tableListDetaildel[0].rows.filter(item=>item.id!==data.id)
        // (item=>item.id!==data.id)
        result.success = true;
        result.id = pid;
      }
      break;
    case 'post':// 新增
      const tableListDetailadd = tableDepartDetail.filter(item => item.id===pid);
      if(tableListDetailadd.length){
        tableListDetailadd[0].rows.unshift({
          ...data,
          id:pid + Math.round(Math.random()*100)
        });
        result.success = true;
        result.id = pid;
      }
      break;
    case 'patch':// 修改
      const tableListDetailupdate = tableDepartDetail.filter(item => item.id===pid);
      let isExist = false
      tableListDetailupdate[0].rows = tableListDetailupdate[0].rows.map(item=>{
        if(item.id ===data.id){
          isExist = true;
          return Object.assign({},item,data)
        }
        return item;
      })
      if(isExist){
        result.success = true;
        result.id = pid;
      }
      break;
    default:
      break;
  }
  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}




export default{
  getSysDepart,
  getSysDepartPerson,
  postSysDepartPerson,
}





